package com.ruoyi.project.system.tatic.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.tatic.domain.req.FindChannelPromotionRepeatTaticReq;
import com.ruoyi.project.system.tatic.domain.resp.FindChannelPromotionRepeatTaticResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.tatic.mapper.TChannelPromotionRepeatTaticMapper;
import com.ruoyi.project.system.tatic.domain.TChannelPromotionRepeatTatic;
import com.ruoyi.project.system.tatic.service.ITChannelPromotionRepeatTaticService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.util.ObjectUtils;

/**
 * 渠道推广资源重复率统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-28
 */
@Service
public class TChannelPromotionRepeatTaticServiceImpl implements ITChannelPromotionRepeatTaticService 
{
    @Autowired
    private TChannelPromotionRepeatTaticMapper tChannelPromotionRepeatTaticMapper;

    /**
     * 查询渠道推广资源重复率统计
     * 
     * @param id 渠道推广资源重复率统计主键
     * @return 渠道推广资源重复率统计
     */
    @Override
    public TChannelPromotionRepeatTatic selectTChannelPromotionRepeatTaticById(Long id)
    {
        return tChannelPromotionRepeatTaticMapper.selectTChannelPromotionRepeatTaticById(id);
    }

    /**
     * 查询渠道推广资源重复率统计列表
     * 
     * @param tChannelPromotionRepeatTatic 渠道推广资源重复率统计
     * @return 渠道推广资源重复率统计
     */
    @Override
    public List<TChannelPromotionRepeatTatic> selectTChannelPromotionRepeatTaticList(TChannelPromotionRepeatTatic tChannelPromotionRepeatTatic)
    {
        return tChannelPromotionRepeatTaticMapper.selectTChannelPromotionRepeatTaticList(tChannelPromotionRepeatTatic);
    }

    /**
     * 新增渠道推广资源重复率统计
     * 
     * @param tChannelPromotionRepeatTatic 渠道推广资源重复率统计
     * @return 结果
     */
    @Override
    public int insertTChannelPromotionRepeatTatic(TChannelPromotionRepeatTatic tChannelPromotionRepeatTatic)
    {
        tChannelPromotionRepeatTatic.setIntervalAddResourceDiscountCount(new BigDecimal(tChannelPromotionRepeatTatic.getIntervalAddResourceCount()).multiply(tChannelPromotionRepeatTatic.getDiscountRate()).setScale(BigDecimal.ROUND_HALF_EVEN).longValue());

        tChannelPromotionRepeatTatic.setIntervalActualAddResourceDiscountCount(new BigDecimal(tChannelPromotionRepeatTatic.getIntervalActualAddResourceCount()).multiply(tChannelPromotionRepeatTatic.getDiscountRate()).setScale(BigDecimal.ROUND_HALF_EVEN).longValue());
        tChannelPromotionRepeatTatic.setIntervalAllAddResourceDiscountCount(new BigDecimal(tChannelPromotionRepeatTatic.getIntervalAllAddResourceCount()).multiply(tChannelPromotionRepeatTatic.getDiscountRate()).setScale(BigDecimal.ROUND_HALF_EVEN).longValue());



        tChannelPromotionRepeatTatic.setCreateTime(DateUtils.getNowDate());
        return tChannelPromotionRepeatTaticMapper.insertTChannelPromotionRepeatTatic(tChannelPromotionRepeatTatic);
    }

    /**
     * 修改渠道推广资源重复率统计
     * 
     * @param tChannelPromotionRepeatTatic 渠道推广资源重复率统计
     * @return 结果
     */
    @Override
    public int updateTChannelPromotionRepeatTatic(TChannelPromotionRepeatTatic tChannelPromotionRepeatTatic)
    {
        tChannelPromotionRepeatTatic.setIntervalAddResourceDiscountCount(new BigDecimal(tChannelPromotionRepeatTatic.getIntervalAddResourceCount()).multiply(tChannelPromotionRepeatTatic.getDiscountRate()).setScale(BigDecimal.ROUND_HALF_EVEN).longValue());
        tChannelPromotionRepeatTatic.setIntervalActualAddResourceDiscountCount(new BigDecimal(tChannelPromotionRepeatTatic.getIntervalActualAddResourceCount()).multiply(tChannelPromotionRepeatTatic.getDiscountRate()).setScale(BigDecimal.ROUND_HALF_EVEN).longValue());
        tChannelPromotionRepeatTatic.setIntervalAllAddResourceDiscountCount(new BigDecimal(tChannelPromotionRepeatTatic.getIntervalAllAddResourceCount()).multiply(tChannelPromotionRepeatTatic.getDiscountRate()).setScale(BigDecimal.ROUND_HALF_EVEN).longValue());
        return tChannelPromotionRepeatTaticMapper.updateTChannelPromotionRepeatTatic(tChannelPromotionRepeatTatic);
    }

    /**
     * 批量删除渠道推广资源重复率统计
     * 
     * @param ids 需要删除的渠道推广资源重复率统计主键
     * @return 结果
     */
    @Override
    public int deleteTChannelPromotionRepeatTaticByIds(String ids)
    {
        return tChannelPromotionRepeatTaticMapper.deleteTChannelPromotionRepeatTaticByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除渠道推广资源重复率统计信息
     * 
     * @param id 渠道推广资源重复率统计主键
     * @return 结果
     */
    @Override
    public int deleteTChannelPromotionRepeatTaticById(Long id)
    {
        return tChannelPromotionRepeatTaticMapper.deleteTChannelPromotionRepeatTaticById(id);
    }

    @Override
    public List<FindChannelPromotionRepeatTaticResp> findChannelPromotionRepeatTatic(FindChannelPromotionRepeatTaticReq req) {
        if(!ObjectUtils.isEmpty(req) && !ObjectUtils.isEmpty(req.getChannelName())){
            req.setChannelNameList(req.getChannelName().split(","));
            req.setChannelName(null);
        }
        List<FindChannelPromotionRepeatTaticResp> list = tChannelPromotionRepeatTaticMapper.findChannelPromotionRepeatTatic(req);
        if(ObjectUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        //区间段新增资源数(未去重)
        Integer intervalAddResourceCount =0;
        //渠道实际新增资源数(去重)
        Integer intervalActualAddResourceCount=0;
        //全库实际新增资源数(去重)
        Integer intervalAllAddResourceCount=0;
        for (FindChannelPromotionRepeatTaticResp resp:list){
            intervalAddResourceCount+=resp.getIntervalAddResourceCount();
            intervalActualAddResourceCount+=resp.getIntervalActualAddResourceCount();
            intervalAllAddResourceCount+=resp.getIntervalAllAddResourceCount();
        }

        FindChannelPromotionRepeatTaticResp resp=new FindChannelPromotionRepeatTaticResp();
        resp.setChannelName("合计");
        resp.setIntervalAddResourceCount(intervalAddResourceCount);
        resp.setIntervalActualAddResourceCount(intervalActualAddResourceCount);
        resp.setIntervalAllAddResourceCount(intervalAllAddResourceCount);
        list.add(resp);
        return list;
    }

    @Override
    public List<String> findChannel() {
        return tChannelPromotionRepeatTaticMapper.findChannel();
    }
}
